package com.lyfx.ly.service.impl;

import com.lyfx.ly.dao.ClassCategoryDao;
import com.lyfx.ly.dao.ClassCourseDao;
import com.lyfx.ly.entity.*;
import com.lyfx.ly.service.ClassCourseAttrValuesService;
import com.lyfx.ly.service.ClassCourseEstypeService;
import com.lyfx.ly.service.ClassCourseService;
import com.lyfx.ly.vo.CourseWithCatVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyfx.common.utils.PageUtils;
import com.lyfx.common.utils.Query;

import com.lyfx.ly.dao.ClassCourseCategoryDao;
import com.lyfx.ly.service.ClassCourseCategoryService;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;


@Service("classCourseCategoryService")
public class ClassCourseCategoryServiceImpl extends ServiceImpl<ClassCourseCategoryDao, ClassCourseCategoryEntity> implements ClassCourseCategoryService {

    @Autowired
    ClassCourseDao classCourseDao;
    @Autowired
    ClassCategoryDao classCategoryDao;

    @Autowired
    ClassCourseService classCourseService;

    @Autowired
    ClassCourseAttrValuesService classCourseAttrValuesService;
    @Autowired
    ClassCourseEstypeService classCourseEstypeService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ClassCourseCategoryEntity> page = this.page(
                new Query<ClassCourseCategoryEntity>().getPage(params),
                new QueryWrapper<ClassCourseCategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(ClassCourseCategoryEntity classCourseCategory) {
        Long courseId = classCourseCategory.getCourseId();
        Long categoryId = classCourseCategory.getCategoryId();
        ClassCourseEntity classCourseEntity = classCourseDao.selectById(courseId);
        ClassCategoryEntity classCategoryEntity = classCategoryDao.selectById(categoryId);
        classCourseCategory.setCourseName(classCourseEntity.getName());
        classCourseCategory.setCategoryName(classCategoryEntity.getName());
        this.save(classCourseCategory);
    }

    @Override
    public void updateCourse(Long courseId, String name) {
        ClassCourseCategoryEntity classCourseCategoryEntity = new ClassCourseCategoryEntity();
        classCourseCategoryEntity.setCourseId(courseId);
        classCourseCategoryEntity.setCourseName(name);
        this.update(classCourseCategoryEntity,new QueryWrapper<ClassCourseCategoryEntity>().eq("course_id", courseId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        ClassCourseCategoryEntity classCourseCategoryEntity = new ClassCourseCategoryEntity();
        classCourseCategoryEntity.setCategoryId(catId);
        classCourseCategoryEntity.setCategoryName(name);
        this.update(classCourseCategoryEntity,new QueryWrapper<ClassCourseCategoryEntity>().eq("category_id",catId));
    }

    /**
     * 获取三级分类下id的所有课程信息
     */
    @Override
    public List<ClassCourseEntity> getCoursesByCatId(Long catId) {
        List<ClassCourseCategoryEntity> entities = this.baseMapper.selectList(new QueryWrapper<ClassCourseCategoryEntity>().eq("category_id", catId));
        List<ClassCourseEntity> collect = entities.stream().map(item -> {
            Long courseId = item.getCourseId();
            ClassCourseEntity byId = classCourseService.getById(courseId);
            return byId;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     *      * key: 你好
     *      * courseId: 2
     *      * categoryId: 8
     *      * page: 1
     *      * limit: 10
     */
    @Override
    public PageUtils queryPageInfo(Map<String, Object> params) {
        QueryWrapper<ClassCourseCategoryEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((w)->{
                w.like("category_name", key).or().like("course_name", key);
            });
        }

        String courseId = (String) params.get("courseId");
        if(!StringUtils.isEmpty(courseId) && !courseId.equals("0")){
            wrapper.eq("course_id", courseId);
        }
        String categoryId = (String) params.get("categoryId");
        if(!StringUtils.isEmpty(categoryId) && !categoryId.equals("0")){
            wrapper.eq("category_id", categoryId);
        }
        List<ClassCourseCategoryEntity> records = this.list(wrapper);
        List<Long> longList = records.stream().map(ClassCourseCategoryEntity::getCourseId).collect(Collectors.toList());
        //去属性值表里查询此课程属性有没有录入
        List<ClassCourseAttrValuesEntity> course_id = classCourseAttrValuesService.list(new QueryWrapper<ClassCourseAttrValuesEntity>().in("course_id", longList));
        List<Long> courseList = course_id.stream().map(ClassCourseAttrValuesEntity::getCourseId).collect(Collectors.toList());

        IPage<ClassCourseEntity> page = classCourseService.page(
                new Query<ClassCourseEntity>().getPage(params),
                new QueryWrapper<ClassCourseEntity>().in("course_id",courseList)
        );


        Map<Long, ClassCourseEntity> map = page.getRecords().stream().collect(Collectors.toMap(ClassCourseEntity::getCourseId, a -> a));
        List<CourseWithCatVo> collect = records.stream().filter(item->{
            return map.containsKey(item.getCourseId());
        }).map(obj->{
            CourseWithCatVo courseWithCatVo = new CourseWithCatVo();
            ClassCourseEntity byId = map.get(obj.getCourseId());
            BeanUtils.copyProperties(byId, courseWithCatVo);
            courseWithCatVo.setCategoryId(obj.getCategoryId());
            courseWithCatVo.setCatname(obj.getCategoryName());
            ClassCourseEstypeEntity one = classCourseEstypeService.getOne(new QueryWrapper<ClassCourseEstypeEntity>().eq("course_id", obj.getCourseId()));
            courseWithCatVo.setEsType(one.getEsType());
            return courseWithCatVo;
        }).collect(Collectors.toList());
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }



}